package com.tg.app.activity.device.add;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.base.BaseActivity;
import com.hjq.permissions.Permission;
import com.tange.base.toolkit.PhoneUtils;
import com.tg.app.R;
import com.tg.appcommon.android.LanguageUtils;
import com.tg.appcommon.android.PermissionUtil;
import com.tg.appcommon.android.TGLog;

public  class LocationGrantGuideActivity extends BaseActivity {

    public static final int REQUEST_CODE = 9428;

    private static final int MIN_CHECK_DURATION = 1000;

    public static final String TAG =  "LocationGrantGuideActivityTAG";

    private long lastRejectPermissionTime = 0;

    public static void launch(Activity activity) {
        activity.startActivityForResult(new Intent(activity, LocationGrantGuideActivity.class), REQUEST_CODE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragment_location_grant_guide);
        hideActionBar();
        initView();
        TGLog.i(TAG, "onCreate");
    }

    private void requestPermission() {
        if (System.currentTimeMillis() - lastRejectPermissionTime > MIN_CHECK_DURATION) {

            PermissionUtil permissionUtil = new PermissionUtil(this);
            permissionUtil.checkPermissionFirst(() -> {
                TGLog.i(TAG, "requestPermission: GET");
                setResult(RESULT_OK);
                finish();
            },  () -> {
                TGLog.i(TAG, "requestPermission: REJECT");
                lastRejectPermissionTime = System.currentTimeMillis();
            }, true, null, null, Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION);
        } else {
            TGLog.i(TAG, "requestPermission: skip because just do it");
        }
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        TGLog.i(TAG, "onResume");
        boolean isLocationGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        if (isLocationGranted) {
            setResult(RESULT_OK);
            finish();
        } else {
            requestPermission();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        TGLog.i(TAG, "onPause");
    }

    @Override
    public void onBackPressed() {
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            setResult(RESULT_CANCELED);
            finish();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    protected void initView() {
        RelativeLayout toolbar = findViewById(R.id.toolbar);
        TextView title = toolbar.findViewById(R.id.device_name);
        title.setText(R.string.please_enable_location_permission);
        modifyToolBar();

        findViewById(R.id.back_toolbar).setOnClickListener(v -> {
            setResult(RESULT_CANCELED);
            finish();
        });

        findViewById(R.id.btn_go_to_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionUtil permissionUtil = new PermissionUtil(LocationGrantGuideActivity.this);
                permissionUtil.openPermission();
            }
        });

        TextView guideTv = findViewById(R.id.guide_tv);
        if (LanguageUtils.isSimChinese(this)) {
            if (PhoneUtils.isMi()) {
                guideTv.setText(R.string.enable_location_permission_detail_tips_xiaomi);
            } else if (PhoneUtils.isHW()) {
                guideTv.setText(R.string.enable_location_permission_detail_tips_huawei);
            } else if (PhoneUtils.isOppo()) {
                guideTv.setText(R.string.enable_location_permission_detail_tips_oppo);
            } else {
                guideTv.setText(R.string.enable_location_permission_detail_tips);
            }
        } else {
            guideTv.setText(R.string.enable_location_permission_detail_tips);
        }
    }
}
